import java.sql.*;

/**
 * This class contains a patient's treatment record.
 */
public class TreatmentRecord {
	private PatientInfo patientInfo;
	private int appointmentId;
	private VitalSigns vitalSigns;
	private String diagnosis;
	private DoctorsOrders doctorsOrders;

	/**
	 * Creates a new instance of Treatment Record with a given PatientInfo
	 * 
	 * @param PatientInfo
	 *            the patient's info
	 */
	public TreatmentRecord(PatientInfo patientInfo) {
		this.patientInfo = patientInfo;
		this.appointmentId = -1;
		this.vitalSigns = null;
		this.diagnosis = null;
		this.doctorsOrders = null;
	}

	/**
	 * Returns the patient's info
	 * 
	 * @return the patient's info
	 */
	public PatientInfo getPatientInfo() {
		return patientInfo;
	}

	/**
	 * Sets the patient's info
	 * 
	 * @param info
	 *            the patient's info
	 */
	public void setPatientInfo(PatientInfo info) {
		this.patientInfo = info;
	}

	/**
	 * Returns the appointment
	 * 
	 * @return the appointment
	 */
	public Appointment getAppointment() {
		if (this.appointmentId < 1)
		{
			return null;
		}
		return Appointment.readAppointment(appointmentId);
	}

	/**
	 * Sets the appointment
	 * 
	 * @param appointment
	 *            the appointment
	 */
	public void setAppointment(int appointmentId) {
		this.appointmentId = appointmentId;
	}

	/**
	 * Returns the vital signs
	 * 
	 * @return the vital signs
	 */
	public VitalSigns getVitalSigns() {
		return vitalSigns;
	}

	/**
	 * Sets the vital signs
	 * 
	 * @param signs
	 *            the vital signs
	 */
	public void setVitalSigns(VitalSigns signs) {
		this.vitalSigns = signs;
	}

	/**
	 * Returns the diagnosis
	 * 
	 * @return the diagnosis
	 */
	public String getDiagnosis() {
		return diagnosis;
	}

	/**
	 * Sets the diagnosis
	 * 
	 * @param diagnosis
	 *            the diagnosis
	 */
	public void setDiagnosis(String diagnosis) {
		this.diagnosis = diagnosis;
	}

	/**
	 * Returns the doctor's orders
	 * 
	 * @return the doctor's orders
	 */
	public DoctorsOrders getDoctorsOrders() {
		return doctorsOrders;
	}

	/**
	 * Sets the doctor's orders
	 * 
	 * @param orders
	 *            the doctor's orders
	 */
	public void setDoctorsOrders(DoctorsOrders orders) {
		this.doctorsOrders = orders;
	}

	// CRUD functionality goes below here

	// create
	public static boolean createTreatmentRecord(String username,
			int appointmentId, String diagnosis) {
		String sql = "INSERT INTO TreatmentRecord (username, appointment_id, diagnosis) VALUES ('"
				+ username + "', '" + appointmentId + "', '" + diagnosis + "')";
		int rows = Database.query(sql);
		if (rows == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	// read
	/**
	 * public static ResultSet readTreatmentRecord(String username, int
	 * appointmentId){ String sql =
	 * "SELECT * FROM TreatmentRecord WHERE username='" + username +
	 * "' AND appointment_id=" + appointmentId; ResultSet rs =
	 * Database.selectQuery(sql); while(rs.next()){ String diagnosis =
	 * rs.getString("diagnosis"); return rs; }
	 **/

	public static int findTreatmentRecordId(String username, int appointmentId,
			String diagnosis) {
		int foundId = -1;
		String sql = "SELECT record_id FROM TreatmentRecord WHERE username='"
				+ username + "' AND appointment_id='" + appointmentId
				+ "' AND '" + diagnosis + "'";
		ResultSet rs = Database.selectQuery(sql);
		// should be 1 treatment record per record_id
		try {
			while (rs.next()) {
				foundId = rs.getInt("record_id");
			}
		} catch (Exception e) {
		}
		return foundId;
	}

	// this method searches through the db using the record_id primary key
	public static TreatmentRecord readTreatmentRecord(int TreatmentRecordId) {
		String foundUsername = "";
		int foundAppointmentId = 0;
		String foundDiagnosis = "";
		String sql = "SELECT * FROM TreatmentRecord WHERE record_id="
				+ TreatmentRecordId;
		ResultSet rs = Database.selectQuery(sql);
		TreatmentRecord record;
		// should be 1 treatment record per record_id
		try {
			while (rs.next()) {
				foundUsername = rs.getString("username");
				foundAppointmentId = rs.getInt("appointment_id");
				foundDiagnosis = rs.getString("diagnosis");
			}
		} catch (Exception e) {
		}
		PatientInfo info = PatientInfo.readPatientInfo(foundUsername);
		record = new TreatmentRecord(info);
		record.setAppointment(foundAppointmentId);
		record.setDiagnosis(foundDiagnosis);
		return record;
	}

	// update
	public static boolean updateTreatmentRecord(String username,
			int appointmentId, String diagnosis) {
		String sql = "UPDATE TreatmentRecord SET diagnosis='" + diagnosis
				+ "' WHERE username='" + username + "' AND appointment_id="
				+ appointmentId;
		int rows = Database.query(sql);
		if (rows == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	// delete
	public static boolean deleteTreatmentRecord(String username,
			int appointmentId) {
		String sql = "DELETE FROM TreatmentRecord WHERE username='" + username
				+ "' AND appointment_id=" + appointmentId;
		int rows = Database.query(sql);
		if (rows == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}
